package routers

import (
	"github.com/gin-gonic/gin"
	"net/http"
	con "goblog/controllers"
	"fmt"
	"goblog/auth"
)

var router *gin.Engine

func InitRouter() *gin.Engine  {
	router = gin.Default()

	//index
	router.GET("/",Index)

	//------------------------begin user------------------------
	router.GET("/users",con.GetUsers)

	router.GET("/pageUsers",con.GetPageUsers)

	router.GET("/user/:id", con.GetUserById)
	//------------------------end user------------------------

	//获取token
	router.GET("/token", con.GetToken)

	//此路由分组需要权限验证
	authorize := router.Group("/", auth.JWTAuth())
	{

		authorize.GET("user", func(c *gin.Context) {
			claims := c.MustGet("claims").(*auth.CustomClaims)
			fmt.Println(claims.ID)
			c.String(http.StatusOK, claims.Name)
		})
	}

	return router
}

func Index(c *gin.Context)  {
	c.String(http.StatusOK, "welcome goblog!")
}











